home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -websites- / wirenet / files / thor25_arexx.lha / CfgAutoReply.thor < prev    next >
Text File  |  1997-08-25  |  15KB  |  454 lines

  1. /* $VER: CfgAutoReply.thor 1.1 (28.08.96) by Neil Bothwick */
  2. /* Config editor for AutoReply.br, an arexx script for     */
  3. /* Thor to generate an event in reply to an email.         */
  4.  
  5. options results
  6.  
  7. /* :Make sure CfgAutoReply is being run from Thor */
  8. thorport = address()
  9. if left(thorport,5) ~= 'THOR.' then do
  10.     say 'CfgAutoReply.thor must be run from Thor'
  11.     exit 10
  12.     end
  13. ;;
  14. /* :Ensure rexxsupport and bbsread libraries are loaded */
  15. if ~show('L','rexxsupport.library') then call addlib('rexxsupport.library',0,-30)
  16. if ~show('p', 'BBSREAD') then do
  17.     address command
  18.     'run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead'
  19.     'WaitForPort BBSREAD'
  20.     end
  21. ;;
  22. /* :Set parameters */
  23. Changed      = 0
  24. CfgFile      = 'ENV:THOR/AutoReply.cfg'
  25. FieldsMenu   = '"Name" "System" "Conference" "Reply Text" "Subject" "Quote Message" "Quote String" "Signature File" "Header File" "Footer File"'
  26. FieldNames   = '"CONFIG" "SYSTEM" "CONFERENCE" "TEXTFILE" "SUBJECT" "QUOTEMSG" "QUOTESTR" "SIGFILE" "HEADFILE" "FOOTFILE"'
  27. MainMenu     = '"Add config" "Edit config" "Delete config" " " "Use" "Save" " " "Help"'
  28. MainTemplate = 'LINE/M'
  29. MenuTemplate = 'MENU/M'
  30. NameTemplate = 'NAME/M'
  31. address(bbsread)
  32. drop Menu. Fields.
  33. READARGS MainTemplate Menu cmdline MainMenu
  34. if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  35. READARGS MenuTemplate Fields cmdline FieldsMenu
  36. if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  37. READARGS NameTemplate Fields cmdline FieldNames
  38. if RC > 0 then call ExitMsg(BBSREAD.LASTERROR)
  39. ;;
  40. /* :Read config file */
  41. address(thorport)
  42. CfgFileSize = word(statef(CfgFile),2)
  43. BytesRead = 0
  44. drop Config.
  45. CfgNo = 0
  46. if ~open(infile,CfgFile,'r') then do
  47.     REQUESTNOTIFY '"Failed to open an existing configuration file"' '" OK "'
  48.     Config.Count = 0
  49.     end
  50. else do
  51.     /* Open progress window */
  52.     OPENPROGRESS title '"CfgAutoReply.thor"' PT '"Reading configuration"'
  53.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  54.     ProgWin = result
  55.     address(bbsread)
  56.  
  57.     do until eof(infile)
  58.         nextln = readln(infile)
  59.         BytesRead = BytesRead + length(nextln) + 1
  60.         if upper(word(nextln,1)) = 'CONFIG' then do
  61.             CfgNo = CfgNo + 1
  62.             drop Args.
  63.             template = 'NAME/A,VALUE/A'
  64.             READARGS template Args CMDLINE nextln
  65.             if RC > 0 then call ExitMsg('Incorrect line in config:\n'||nextline)
  66.             Config.CfgNo = Args.Value
  67.             do until Args.Name = 'ENDCONFIG'
  68.                 if eof(infile) then call ExitMsg('End of config reached without ENDCONFIG')
  69.                 nextln = readln(infile)
  70.                 BytesRead = BytesRead + length(nextln) + 1
  71.                 drop Args. Config.CfgNo.
  72.                 template = 'NAME/A,VALUE'
  73.                 READARGS template Args CMDLINE nextln
  74.                 if RC > 0 then call ExitMsg('Incorrect line in config:\n'||nextline)
  75.                 if Args.Name = 'CONFIG'     then Config.CfgNo = Args.Value
  76.                 else do i = 2 to Fields.Name.Count
  77.                     if Args.Name = Fields.Name.i then do
  78.                         interpret 'Config.CfgNo.'fields.Name.i' = Args.Value'
  79.                         iterate
  80.                         end
  81.                     end
  82.                 end
  83.  
  84.             /* Update progress bar */
  85.             address(thorport)
  86.             UPDATEPROGRESS req ProgWin current BytesRead*100%CfgFileSize
  87.             address(bbsread)
  88.             end
  89.         end
  90.     call close(infile)
  91.     Config.Count = CfgNo
  92.     address(thorport)
  93.     CLOSEPROGRESS req ProgWin
  94.     end
  95. ;;
  96. /* :Main loop */
  97. address(thorport)
  98. do until pos(upper(Choice),'SAVEUSE') > 0
  99.     Choice = ShowMain()
  100.     select
  101.         when Choice = Menu.Line.1 then call AddCfg
  102.         when Choice = Menu.Line.2 then call EditCfg(0)
  103.         when Choice = Menu.Line.3 then call DeleteCfg
  104.         when Choice = 'Use'  then call UseCfg
  105.         when Choice = 'Save' then call SaveCfg
  106.         when Choice = 'Help' then call ShowHelp
  107.         otherwise leave
  108.         end
  109.     end
  110. ;;
  111. /* :Exit, after checking if config should be saved */
  112. if Changed = 1 then REQUESTNOTIFY '"You have changed the configuration.\nDo you want to save it before exiting?"' '"_Save|_Use|E_xit"'
  113. if RC = 30 then call ExitMsg(THOR.LASTERROR)
  114. select
  115.     when result = 1 then call SaveCfg
  116.     when result = 2 then call UseCfg
  117.     otherwise nop
  118.     end
  119.  
  120. exit
  121. ;;
  122.  
  123. /* ========== Procedures ========== */
  124.  
  125. /* :Exit with error message */
  126. ExitMsg:
  127.     address(thorport)
  128.     parse arg errmsg
  129.     REQUESTNOTIFY '"'errmsg'"' '"Abort"'
  130.     exit
  131. ;;
  132. /* :Show main menu */
  133. ShowMain:
  134.     address(thorport)
  135.     do until result ~= ' '
  136.         drop PICKED.
  137.         REQUESTLIST instem Menu.LINE TITLE '"CfgAutoReply.thor    "' SIZEGADGET
  138.         end
  139.     if RC = 5 then return 'QUIT'
  140.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  141.     return result
  142. ;;
  143. /* :Edit a configuration */
  144. EditCfg:
  145.     arg CfgNo
  146.     OldChanged = Changed
  147.  
  148.     /* Select config to edit if not specified */
  149.     if CfgNo = 0 then do
  150.         if Config.Count = 0 then do                         /* Return if no configs defined */
  151.             REQUESTNOTIFY '"You need to create a configuration\nitem before you can edit it ;-)"' '" OK "'
  152.             return 5
  153.             end
  154.  
  155.         REQUESTLIST instem Config title '"Choose config to edit"'
  156.         if RC = 5 then return 5
  157.         if RC > 0 then call ExitMsg(THOR.LASTERROR)
  158.         EditCfg = result
  159.         do i = 1 to Config.Count
  160.             if Config.i = EditCfg then leave
  161.             end
  162.         CfgNo = i
  163.         end
  164.  
  165.     /* Copy values to temporary variable for editing */
  166.     drop EditTemp.
  167.     EditTemp.Config = Config.CfgNo
  168.     do i = 2 to Fields.Name.Count
  169.         if symbol('Config.CfgNo.'Fields.Name.i) = 'VAR' then interpret 'EditTemp.'Fields.Name.i' = Config.CfgNo.'Fields.Name.i
  170.         else interpret 'EditTemp.'Fields.Name.i' = ""'
  171.         end
  172.  
  173.     /* Display edit menu */
  174.     do forever
  175.         /* Set up menu */
  176.         drop EditMenu.
  177.         EditMenu.Count = Fields.Name.Count + 4
  178.         do i = 1 to Fields.Name.Count
  179.             interpret 'EditMenu.i = left(Fields.Menu.i||":                         ",22)||EditTemp.'Fields.Name.i
  180.             end
  181.  
  182.         interpret 'EditMenu.'EditMenu.Count-3' = ""'
  183.         interpret 'EditMenu.'EditMenu.Count-2' = "Accept"'
  184.         interpret 'EditMenu.'EditMenu.Count-1' = ""'
  185.         interpret 'EditMenu.'EditMenu.Count' = "Help"'
  186.  
  187.         /* Select field to edit */
  188.         REQUESTLIST instem EditMenu title '"Choose a field to edit"'
  189.         if RC = 5 then do
  190.             Changed = OldChanged
  191.             return 5
  192.             end
  193.         if RC > 0 then call ExitMsg(THOR.LASTERROR)
  194.         EditItem = result
  195.  
  196.         select
  197.             when EditItem = ''       then iterate
  198.             when EditItem = 'Help'   then do
  199.                 call ShowHelp
  200.                 iterate
  201.                 end
  202.             when EditItem = 'Accept' then leave
  203.             otherwise do
  204.                 do i = 1 to EditMenu.Count
  205.                     if EditMenu.i = EditItem then leave
  206.                     end
  207.                 ItemNo = i
  208.                 end
  209.             end
  210.  
  211.         parse var EditItem EditTitle ':' EditValue
  212.         EditValue = strip(EditValue,L)
  213.  
  214.         /* Call edit procedure for that field */
  215.         do i = 1 to Fields.Name.Count
  216.             if EditTitle = Fields.Menu.i then do
  217.                 interpret 'call Edit'||Fields.Name.i
  218.                 interpret 'EditTemp.'||Fields.Name.i '= EditValue'
  219.                 Changed = 1
  220.                 leave
  221.                 end
  222.             end
  223.  
  224.         end
  225.  
  226.     /* Copy values back from temporary variable */
  227.     Config.CfgNo = EditTemp.Config
  228.     do i = 2 to Fields.Name.Count
  229.         interpret 'tmp = EditTemp.'Fields.Name.i
  230.         if tmp > '' then interpret 'Config.CfgNo.'Fields.Name.i' = tmp'
  231.         else interpret 'drop Config.CfgNo.'Fields.Name.i
  232.         end
  233.  
  234.     return 0
  235. ;;
  236. /* :Add a configuration */
  237. AddCfg:
  238.     EditValue = ''
  239.     call EditCONFIG                                         /* Get config name */
  240.     if EditValue = '' then return
  241.     do i = 1 to Config.Count                                /* Check this name is not already used */
  242.         if upper(EditValue) = upper(Config.i) then do
  243.             'REQUESTNOTIFY "There is already a configuration item called 'Config.i'." bt " OK "'
  244.             return
  245.             end
  246.         end
  247.     CfgNo = Config.Count+1
  248.     Config.CfgNo = EditValue
  249.     EditValue = ''
  250.     call EditSYSTEM                                         /* Get system */
  251.     if EditValue = '' then return
  252.     Config.CfgNo.SYSTEM = EditValue
  253.     EditValue = ''
  254.     call EditCONFERENCE                                     /* Conference */
  255.     if EditValue = '' then return
  256.     Config.CfgNo.CONFERENCE = EditValue
  257.     EditValue = ''
  258.     call EditTEXTFILE                                       /* Reply text */
  259.     if EditValue = '' then return
  260.     Config.CfgNo.TEXTFILE = EditValue
  261.     if EditCfg(CfgNo) = 0 then do                           /* Edit configuration */
  262.         Changed = 1
  263.         Config.Count = CfgNo
  264.         end
  265.     return
  266. ;;
  267. /* :Alter individual configuration items */
  268. EditCONFIG:
  269.     REQUESTSTRING title '"CfgAutoReply"' body '"Name of configuration item   "' BT '" _OK |Cancel"' ID '"'EditValue'"'
  270.     if RC = 5 then return
  271.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  272.     EditValue = result
  273.     return
  274.  
  275. EditSYSTEM:
  276.     address(bbsread)
  277.     drop BBSS.
  278.     GETBBSLIST stem BBSS
  279.     if RC = 30 then call ExitMsg(BBSREAD.LASTERROR)
  280.     address(thorport)
  281.     REQUESTLIST instem BBSS title '"Name of system containing incoming messages   "'
  282.  
  283.     if RC = 5 then return
  284.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  285.     EditValue = result
  286.     return
  287.  
  288. EditCONFERENCE:
  289.     address(bbsread)
  290.     drop CONFS.
  291.     GETCONFLIST bbsname '"'Config.CfgNo.System'"' stem CONFS
  292.     if RC = 30 then call ExitMsg(BBSREAD.LASTERROR)
  293.     address(thorport)
  294.     REQUESTLIST instem CONFS title '"Name of conference containing incoming messages   "'
  295.     if RC = 5 then return
  296.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  297.     EditValue = result
  298.     return
  299.  
  300. EditTEXTFILE:
  301.     call SplitPath()
  302.     REQUESTFILE title '"Please select the reply text file"' ID '"'path'"' IF '"'file'"' FULLPATH
  303.     if RC = 5 then return
  304.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  305.     EditValue = result
  306.     return
  307.  
  308. EditSUBJECT:
  309.     REQUESTSTRING title '"CfgAutoReply"' body '"Subject line for reply\nUse %%s to include original subject\nDefault is Re: %s"' BT '" _OK |Cancel"' ID '"'EditValue'"'
  310.     if RC = 5 then return
  311.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  312.     EditValue = result
  313.     return
  314.  
  315. EditQUOTEMSG:
  316.     REQUESTNOTIFY '"Where would you like the quote the incoming mail,\n above your reply, below it, or not at all?"' '"_Above|_Below|_NoQuote"'
  317.     if RC = 5 then return
  318.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  319.     select
  320.         when result = 1 then EditValue = 'Above'
  321.         when result = 2 then EditValue = 'Below'
  322.         otherwise EditValue = ''
  323.         end
  324.     return
  325.  
  326. EditQUOTESTR:
  327.     REQUESTSTRING title '"CfgAutoReply"' body '"The string or character prepended to quoted lines"' BT '" _OK |Cancel"' ID '"'EditValue'"'
  328.     if RC = 5 then return
  329.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  330.     EditValue = result
  331.     return
  332.  
  333. EditSIGFILE:
  334.     call SplitPath()
  335.     REQUESTFILE title '"Please select signature file"' ID '"'path'"' IF '"'file'"' FULLPATH
  336.     if RC = 5 then return
  337.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  338.     EditValue = result
  339.     return
  340.  
  341. EditHEADFILE:
  342.     call SplitPath()
  343.     REQUESTFILE title '"Please select a header file"' ID '"'path'"' IF '"'file'"' FULLPATH
  344.     if RC = 5 then return
  345.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  346.     EditValue = result
  347.     return
  348.  
  349. EditFOOTFILE:
  350.     call SplitPath()
  351.     REQUESTFILE title '"Please select a footer file"' ID '"'path'"' IF '"'file'"' FULLPATH
  352.     if RC = 5 then return
  353.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  354.     EditValue = result
  355.     return
  356. ;;
  357. /* :Split the current EditValue into path and file */
  358. SplitPath:
  359.     select
  360.         when lastpos('/',EditValue) > 0 then do
  361.             pathend = lastpos('/',EditValue)
  362.             path = left(EditValue,pathend-1)
  363.             file = substr(EditValue,pathend+1)
  364.             end
  365.         when lastpos(':',EditValue) > 0 then do
  366.             pathend = lastpos(':',EditValue)
  367.             path = left(EditValue,pathend)
  368.             file = substr(EditValue,pathend+1)
  369.             end
  370.         when EditValue = '' then do
  371.             Path = 'RAM:'
  372.             File = ''
  373.             end
  374.         otherwise do
  375.             path = ''
  376.             file = EditValue
  377.             end
  378.         end
  379.     return
  380. ;;
  381. /* :Delete a configuration */
  382. DeleteCfg:
  383.     /* Select config to delete */
  384.     REQUESTLIST instem Config title '"Choose configuration to delete  "'
  385.     if RC = 5 then return
  386.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  387.     DelCfg = result
  388.     REQUESTNOTIFY '"'DelCfg||':\nAre you sure you want to delete this configuration?"' '"_Delete|_Cancel"'
  389.     if result = 0 then return
  390.     Changed = 1
  391.     do i = 1 to Config.Count
  392.         if Config.i = DelCfg then leave
  393.         end
  394.     CfgNo = i
  395.  
  396.     Config.Count = Config.Count - 1
  397.     if CfgNo <= Config.Count then do i = CfgNo to Config.Count
  398.         x = i+1
  399.         Config.i = Config.x
  400.         do j = 1 to Fields.Name.Count
  401.             if symbol('Config.x.'Fields.Name.j) = 'VAR' then do
  402.                 interpret 'Config.i.'Fields.Name.j '= Config.x.'Fields.Name.j
  403.                 end
  404.             else do
  405.                 interpret 'drop Config.i.'Fields.Name.j
  406.                 end
  407.             end
  408.         end
  409.  
  410.     return
  411. ;;
  412. /* :Save the config to ENV: */
  413. UseCfg:
  414.     if exists(CfgFile) then address command 'copy >NIL:' CfgFile CfgFile'.bak'
  415.     if ~open(cfg,CfgFile,'w') then call ExitMsg('Failed to open configuration file to save')
  416.  
  417.     /* Open progress window */
  418.     OPENPROGRESS title '"CfgAutoReply.thor"' PT '"Writing configuration"'
  419.     if RC > 0 then call ExitMsg(THOR.LASTERROR)
  420.     ProgWin = result
  421.  
  422.     do i = 1 to Config.Count
  423.         call writeln(cfg,'CONFIG       "'Config.i'"')
  424.         do j = 2 to Fields.Name.Count
  425.             if symbol('Config.i.'Fields.Name.j) ~= 'VAR' then iterate
  426.             CfgName = left(Fields.Name.j||'             ',13)
  427.             interpret 'CfgVal = Config.i.'Fields.Name.j
  428.             call writeln(cfg,CfgName'"'CfgVal'"')
  429.             end
  430.             call writeln(cfg,'ENDCONFIG')
  431.             call writeln(cfg,'')
  432.  
  433.         /* Update progress bar */
  434.         UPDATEPROGRESS req ProgWin current i*100%Config.Count
  435.         end
  436.     call close(cfg)
  437.     CLOSEPROGRESS req ProgWin
  438.     Changed = 0
  439.     return
  440. ;;
  441. /* :Save the config to ENVARC: */
  442. SaveCfg:
  443.     call UseCfg()
  444.     address command 'copy >NIL:' CfgFile'#? TO ENVARC:THOR'
  445.     return
  446. ;;
  447. /* :Call multiview to display help file */
  448. ShowHelp:
  449.     drop GLOBALCFG.
  450.     GETGLOBALCONFIG stem GLOBALCFG              /* Get global information */
  451.     address command 'MultiView `GetEnv THOR/THORPath`docs/AutoReply.guide PUBSCREEN' GLOBALCFG.PUBSCREENNAME
  452.     return
  453. ;;
  454.